#!/usr/bin/env python2.7

# Copyright 2016 Nidium Inc. All rights reserved.
# Use of this source code is governed by a MIT license
# that can be found in the LICENSE file.

from konstructor import Build
from konstructor import Builder
from konstructor import Konstruct
from konstructor import Variables
from konstructor import Deps
from konstructor import CommandLine
from konstructor import Tests

import os

CONFIGURE_PATH = os.path.dirname(os.path.realpath(__file__))

Gyp = Builder.Gyp

Gyp.set("nidium_js_disable_window_global", 1)
Gyp.set("nidium_product_define", "NIDIUM_PRODUCT_SERVER")

Variables.set('product', 'nidiumserver')

Deps.set(
    Deps.Konstruct("libnidiumcore", "configure_libnidiumcore"),
)


@Deps.register("jemalloc")
def jemalloc():
    return {
        "location": Deps.GitRepo("https://github.com/jemalloc/jemalloc.git", tag="4.1.0"),
        "build": ["./autogen.sh", "./configure --enable-prof", "make"],
        "outputs": ["lib/libjemalloc.a"]
    }

@CommandLine.option("--jemalloc", default=False)
def jemalloc(jemalloc):
    if jemalloc:
        Deps.set("jemalloc")
        Gyp.set("jemalloc", 1)

@CommandLine.option("--no-fork", default=False)
def nofork(nofork):
    if nofork:
        Gyp.set("nofork", 1)

@CommandLine.option("--heap-profiling", default=False)
def heapProfiling(profiling):
    if profiling:
        jemalloc(True)
        Deps.set("gperftools")



@CommandLine.option("--unit-tests", default=False)
@CommandLine.option("--auto-tests", default=False)
def testServer(unitTests, autoTests):
    tests = []

    if unitTests:
        tests.append(("../../bin/nidium-server unittests.js", "tests/jsunittest/"))

    # XXX : Nidium server needs only a subset of the auto tests. How to handle this ?
    # if autoTests:
    #     tests.append("dist/nidium-server nativejscore/var/js/tests/autotests/auto_suites.js")

    Tests.register(tests)

Gyp.setArgs("--depth ./ --include=gyp/config.gypi --include=gyp/common.gypi")
Build.add(Gyp("gyp/nidium-server.gyp"));

if __name__ == '__main__':
    Konstruct.start()
